package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.entity.Result;
import org.dey.mapper.RiskAssessmentMapper;
import org.dey.pojo.Order;
import org.dey.pojo.PageVo;
import org.dey.pojo.RiskAssessment;
import org.dey.service.NumbersService;
import org.dey.service.OrderService;
import org.dey.service.RiskAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RiskAssessmentServiceImpl extends ServiceImpl<RiskAssessmentMapper, RiskAssessment> implements RiskAssessmentService {
    @Autowired
    RiskAssessmentMapper riskAssessmentMapper;

    @Autowired
    NumbersService numbersService;

    @Autowired
    OrderService orderService;

    @Override
    public RiskAssessment queryRaById(int raidd) {
        System.out.println("------------------------>"+raidd);
        return this.getById(raidd);
    }

    @Override
    public PageVo<RiskAssessment> queryRa(RiskAssessment riskAssessment, int pageNum, int pagesize) {

        PageHelper.startPage(pageNum, pagesize);

        List<RiskAssessment> list = riskAssessmentMapper.queryRa();

        PageInfo<RiskAssessment> pageInfo = new PageInfo<>(list);

        PageVo<RiskAssessment> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public Result deleteRa(int raidd) {
        Result result = null;
        try {
            this.riskAssessmentMapper.deleteByRaid(raidd);
            result = new Result(true, "删除成功");
        } catch (Exception e) {
            result = new Result(true, "删除失败");
        }
        return result;
    }

    @Override
    public Result addRa(RiskAssessment riskAssessment) {
        Result result = null;
        int num=riskAssessment.getId();
//        riskAssessment.setRaid(numbersService.memberid());
        Date time = new Date();
        riskAssessment.setDateOfAssessment(time);
        boolean one = this.save(riskAssessment);
        if (one) {
            UpdateWrapper<Order> updateWrapper = new UpdateWrapper<Order>();
            updateWrapper.eq("id", num).setSql("orderStatus='已到诊'");
            boolean update = orderService.update(updateWrapper);
            if (update) {
                result = new Result(true, "中医体质辨识添加成功");
            }

        } else {
            result = new Result(true, "中医体质辨识添加失败");
        }
        //前端弹框取值==> res.data.message
        return result;
    }

    @Override
    public int updateRa(RiskAssessment riskAssessment) {
        int num = 0;
        boolean b = this.updateById(riskAssessment);
        if (b) {
            num = 1;
        }
        return num;
    }


}
